<%@LANGUAGE="JAVASCRIPT" %>
<!--#include file ="../Includes/JSON2.asp"-->
<!--#include file ="../Includes/DB.asp"--> 
<!--#include file ="../Includes/ASPHelpers.asp"-->

<%


var API = {
	NoMethod: function(){
		var ret = {};
		ret.Success = false;
		ret.ErrorMessage = "No Method Passed";
			
		return JSON.stringify(ret);
	},
		
	/*----------------------------------------------------------------------*/
	MethodNotFound: function(){
		var ret = {};
		ret.Success = false;
		ret.ErrorMessage = "AJAX method '" + originalMethod + "' does not exist.";
			
		return JSON.stringify(ret);
	},
		
	/*----------------------------------------------------------------------*/
	GetScriptBlockAndDecisions: function(){
		var ret = {};
		ret.Success = "";
		ret.Data = {};
		ret.ErrorMessage = "";
			
		var scriptBlockID = 	new String(Request.Form("ScriptBlockID"));
			
		if(Helpers.IsNullOrEmpty(scriptBlockID)){
			ret.Success = 		false;
			ret.ErrorMessage = 	"No ScriptBlockID sent.";
				
			return JSON.stringify(ret);
		}
		
		queryName = 	"qryGetScriptBlockByScriptBlockID";
			
		var db, rs;
		db = new DB.DataCall();
		db.QueryName = 			queryName;
		db.ConnectionString = 	DB.ConnectionStrings.Scripts;
		db.CommandType = 		DB.CommandTypes.StoredProc;
			
		db.AddParameter("@ScriptBlockID", DB.DataTypes.String, scriptBlockID);
			
		db.Execute();
			
		ret.Success = db.Success;
			
		if(db.Success){
			rs = db.RecordSet;
				
			if(rs.RecordCount > 0){
				rs.MoveFirst();
					
				ret.Data.ScriptBlockID = 	rs.fields("ScriptBlockID").value;
				ret.Data.DecisionID =		rs.fields("DecisionID").value;
				ret.Data.ScriptText = 		Helpers.DecodeHTML(rs.fields("ScriptText").value);
				ret.Data.Decisions =		[];
					
				/* get decisions for this scriptblock */
				queryName = 			"qryGetDecisionsAndChildScriptBlock";
					
				db = new DB.DataCall();
				db.QueryName = 			queryName;
				db.ConnectionString = 	DB.ConnectionStrings.Scripts;
				db.CommandType = 		DB.CommandTypes.StoredProc;
			
				db.AddParameter("@ScriptBlockID", DB.DataTypes.String, ret.Data.ScriptBlockID);
					
				db.Execute();
					
				ret.Success = db.Success;
				ret.Debug = db;
					
				if(db.Success){
					rs = db.RecordSet;
						
					while(!rs.EOF){
						var decision = {
							DecisionID:			rs.fields("DecisionID").value,
							ScriptBlockID:		rs.fields("ScriptBlockID").value,
							ChildScriptBlockID:	rs.fields("ChildScriptBlockID").value || "",
							DecisionTitle:		Helpers.DecodeHTML(rs.fields("DecisionTitle").value)
						}

					ret.Data.Decisions.push(decision);
						
					rs.MoveNext();
					}
				}
				else{
					ret.ErrorMessage = db.ErrorMessage + " (qryGetDecisionsAndChildScriptBlock)";
				}
					
			}
			else{
				ret.Data.ScriptBlockID =	"new";
				ret.Data.DecisionID =		"";
				ret.Data.ScriptText =		"New script.";
				ret.Data.Decisions =		[];
			}
		}
		else{
			ret.ErrorMessage = db.ErrorMessage + " (qryGetScriptBlockByScriptBlockID)";
			ret.Data = db;
		}
			
		return JSON.stringify(ret);
			
	},
		
	/*----------------------------------------------------------------------*/
	GetScripts: function(){
		var ret =           {};
		ret.Success =       "";
		ret.Data =          {};
		ret.ErrorMessage =  "";
			
			
		queryName =          	"qryGetScripts";
			
		var db, rs;
		db = new DB.DataCall();
		db.QueryName = 			queryName;
		db.ConnectionString = 	DB.ConnectionStrings.Scripts;
		db.CommandType = 		DB.CommandTypes.StoredProc;
			

		db.Execute();
		
		ret.Success = db.Success;
		ret.ErrorMessage = db.ErrorMessage;

		if(db.Success){
			rs = db.RecordSet;
            ret.Data.Headers = [];
            ret.Data.Scripts = [];
                
            for(var x = 0, len = rs.fields.count; x < len; x++){
                ret.Data.Headers.push(rs.fields(x).name);
            };
				
			while(!rs.EOF){
				var script = {
					ScriptID:	rs.fields("ScriptID").value,
					Category:	Helpers.DecodeHTML(rs.fields("Category").value),
					Title:	    Helpers.DecodeHTML(rs.fields("Title").value),
					Created:	new String(rs.fields("Created").value),
					CreatedBy:  rs.fields("CreatedBy").value,
					Modified:   new String(rs.fields("Modified").value),
					ModifiedBy: rs.fields("ModifiedBy").value
				}

				ret.Data.Scripts.push(script);
				
				rs.MoveNext();
			}
		}
			
		return JSON.stringify(ret);					    
	},

	/*----------------------------------------------------------------------*/
	GetScriptsGroupedByCategory: function(){
		var db, rs;
		
		var ret =           {};
		ret.Success =       "false";
		ret.Data =          {};
		ret.ErrorMessage =  "";
		

		db = new DB.DataCall();
		db.QueryName =			"qryGetScripts";
		db.ConnectionString =	DB.ConnectionStrings.Scripts;
		db.CommandType =		DB.CommandTypes.StoredProc;
					
		db.Execute();	

		if(db.Success){
			
			var scripts = {};

			if(db.RecordSet.RecordCount > 0){
				var rs = db.RecordSet;
				rs.MoveFirst();
					
				while(!rs.EOF){
					var script = {
						ScriptID:	db.RecordSet.fields("ScriptID").value
						, Title: 	Helpers.DecodeHTML(db.RecordSet.fields("Title").value)
						, Category: Helpers.DecodeHTML(db.RecordSet.fields("Category").value)
					}

					if(!scripts[script.Category])
						scripts[script.Category] = [];

					scripts[script.Category].push(script);
						
					rs.MoveNext();
				}

				ret.Data.Scripts = scripts;
				ret.Success = "true";
	
			}//recordcount > 0
		}//db.Success
		else {
			ret.ErrorMessage = db.ErrorMessage;
		}



		return JSON.stringify(ret);

	},

	/*----------------------------------------------------------------------*/
	SearchForScripts: function(){
		var db, rs;
		
		var ret =           {};
		ret.Success =       "false";
		ret.Scripts =       [];
		ret.ErrorMessage =  "";

		var criteria = 		new String(Request.Form("Criteria"));
		var categoryOnly =	new String(Request.Form("CategoryOnly")) == "true" ? true : false;
		var exactMatch = 	new String(Request.Form("ExactMatch")) == "true" ? true : false;
		
		var query = "qrySearchAll";
		if(categoryOnly) {
			query = "qrySearchCategory";
			if(exactMatch) 
				query = "qrySearchCategoryExactMatch";
		}
		else if(exactMatch) query = "qrySearchExactMatch";

		

		db = new DB.DataCall();
		db.QueryName =			query;
		db.ConnectionString =	DB.ConnectionStrings.Scripts;
		db.CommandType =		DB.CommandTypes.StoredProc;

		db.AddParameter("@Criteria", DB.DataTypes.String, criteria);
					
		db.Execute();

		ret.Data = db;

		ret.Success = db.Success;
		if(db.Success){

			if(db.RecordSet.RecordCount > 0){ 
				var rs = db.RecordSet;
				rs.MoveFirst();

				while(!rs.EOF){ 
					var script = {
						ScriptID:	db.RecordSet.fields("ScriptID").value
						, Title: 	Helpers.DecodeHTML(db.RecordSet.fields("Title").value)
						, MatchType:	Helpers.DecodeHTML(db.RecordSet.fields("MatchType").value)
						, MatchText: db.RecordSet.fields("MatchText").value
					}


					ret.Scripts.push(script);
						
					rs.MoveNext(); 
				}
	
			}//recordcount > 0
		}//db.Success
		else {
			ret.ErrorMessage = db.ErrorMessage;
		}

		return JSON.stringify(ret);
	}


		
}/* END OF AJAX METHODS */


/*----------------------------------------------------------------------*/
var method = Request.QueryString("method") || "NoMethod";
var originalMethod = method;

if(!API[method]){
	method = "MethodNotFound";
}

Response.ContentType = "application/json";
Response.Write(API[method]());
%>
